package Vista;

import Controlador.ControlSucursal;
import Controlador.ControlVendedor;
import Modelo.Sucursal;
import Modelo.Vendedor;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class VendedorRegistrar extends javax.swing.JFrame {
    ControlVendedor vendedor=new ControlVendedor();
    ControlSucursal sucursal= new ControlSucursal();

    //Constructor
     public VendedorRegistrar() throws SQLException {
        initComponents();     
        this.setLocationRelativeTo(null);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextCedula = new javax.swing.JTextField();
        jTextApellido = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextNombre = new javax.swing.JTextField();
        jTextDireccion = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jTextTelefono = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextCodigo = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jTextCodigoSucursal = new javax.swing.JTextField();
        jLabelSucursal = new javax.swing.JLabel();
        jButtonCancelar = new javax.swing.JButton();
        jButtonRegistrar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButtonSalir = new javax.swing.JButton();

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(0, 51, 0), new java.awt.Color(0, 51, 0), null, null));

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(0, 51, 0)));

        jLabel3.setBackground(new java.awt.Color(255, 255, 255));
        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setForeground(new java.awt.Color(51, 0, 0));
        jLabel3.setText("Cedula:");

        jLabel4.setBackground(new java.awt.Color(255, 255, 255));
        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setForeground(new java.awt.Color(51, 0, 0));
        jLabel4.setText("Apellidos:");

        jTextCedula.setEnabled(false);
        jTextCedula.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextCedulaKeyPressed(evt);
            }
        });

        jTextApellido.setEnabled(false);
        jTextApellido.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextApellidoKeyPressed(evt);
            }
        });

        jLabel6.setBackground(new java.awt.Color(255, 255, 255));
        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel6.setForeground(new java.awt.Color(51, 0, 0));
        jLabel6.setText("Nombres");

        jLabel7.setBackground(new java.awt.Color(255, 255, 255));
        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel7.setForeground(new java.awt.Color(51, 0, 0));
        jLabel7.setText("Direccion");

        jTextNombre.setEnabled(false);
        jTextNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextNombreKeyPressed(evt);
            }
        });

        jTextDireccion.setEnabled(false);
        jTextDireccion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextDireccionKeyPressed(evt);
            }
        });

        jLabel11.setBackground(new java.awt.Color(255, 255, 255));
        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel11.setForeground(new java.awt.Color(51, 0, 0));
        jLabel11.setText("Telefono:");

        jTextTelefono.setEnabled(false);
        jTextTelefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextTelefonoKeyPressed(evt);
            }
        });

        jLabel5.setBackground(new java.awt.Color(255, 255, 255));
        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setForeground(new java.awt.Color(51, 0, 0));
        jLabel5.setText("Codigo:");

        jTextCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextCodigoKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addGap(66, 66, 66)
                        .addComponent(jTextTelefono, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel7)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(66, 66, 66)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
                            .addComponent(jTextDireccion, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
                            .addComponent(jTextApellido, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jTextCodigo, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextCedula, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jTextCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jTextNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jTextTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));
        jPanel5.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(0, 51, 0)));

        jLabel9.setBackground(new java.awt.Color(255, 255, 255));
        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel9.setForeground(new java.awt.Color(51, 0, 0));
        jLabel9.setText("Codigo Sucursal:");

        jTextCodigoSucursal.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextCodigoSucursalKeyPressed(evt);
            }
        });

        jLabelSucursal.setBackground(new java.awt.Color(255, 255, 255));
        jLabelSucursal.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabelSucursal.setForeground(new java.awt.Color(51, 102, 0));
        jLabelSucursal.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addGap(18, 18, 18)
                .addComponent(jTextCodigoSucursal, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                .addGap(6, 6, 6)
                .addComponent(jLabelSucursal, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel9)
                        .addComponent(jTextCodigoSucursal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabelSucursal, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButtonCancelar.setFont(new java.awt.Font("Tahoma", 0, 14));
        jButtonCancelar.setForeground(new java.awt.Color(51, 0, 0));
        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });
        jButtonCancelar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButtonCancelarKeyPressed(evt);
            }
        });

        jButtonRegistrar.setFont(new java.awt.Font("Tahoma", 0, 14));
        jButtonRegistrar.setForeground(new java.awt.Color(51, 0, 0));
        jButtonRegistrar.setText("Registrar");
        jButtonRegistrar.setEnabled(false);
        jButtonRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegistrarActionPerformed(evt);
            }
        });
        jButtonRegistrar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButtonRegistrarKeyPressed(evt);
            }
        });

        jLabel1.setBackground(new java.awt.Color(255, 255, 255));
        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 30));
        jLabel1.setForeground(new java.awt.Color(102, 0, 0));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rutascoronasa/resources/Imagenes/registro de fuerza de venta.png"))); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jButtonRegistrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCancelar))
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonCancelar)
                    .addComponent(jButtonRegistrar))
                .addContainerGap())
        );

        jButtonSalir.setForeground(new java.awt.Color(51, 0, 0));
        jButtonSalir.setText("Finalizar");
        jButtonSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonSalir, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalirActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButtonSalirActionPerformed

    private void jButtonRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegistrarActionPerformed
            try {                
                if(!vendedor.isExist(jTextCodigo.getText()))
                    registrar();
                else{
                    JOptionPane.showMessageDialog(null,"Vendedor Se Encuentra Registrado En El Sistema...!");
                }
            } catch (SQLException ex) {
                Logger.getLogger(VendedorConsultar.class.getName()).log(Level.SEVERE, null, ex);
            }
    }//GEN-LAST:event_jButtonRegistrarActionPerformed

    private void jTextCedulaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextCedulaKeyPressed
      if(evt.getKeyCode()==10){
            if(jTextCedula.getText().trim().equalsIgnoreCase(""))
                JOptionPane.showMessageDialog(null,"Este Campo Es Requerido");
            else{
                try {
                    if (vendedor.isExist(jTextCodigo.getText())) {
                        JOptionPane.showMessageDialog(null, "El Vendedor Se Encuentra Registrado");
                    } else {
                        jTextCedula.setEnabled(false);
                        jTextNombre.setEnabled(true);
                        jTextNombre.requestFocus();
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(VendedorRegistrar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }//GEN-LAST:event_jTextCedulaKeyPressed

    private void jTextNombreKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextNombreKeyPressed
        if (evt.getKeyCode()==10){
            if(jTextNombre.getText().trim().equalsIgnoreCase(""))
                JOptionPane.showMessageDialog(null, "Este Campo Es Requerido");
            else {
                jTextApellido.setEnabled(true);
                jTextApellido.requestFocus();
            }
        }
    }//GEN-LAST:event_jTextNombreKeyPressed

    private void jTextApellidoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextApellidoKeyPressed
         if (evt.getKeyCode()==10){
            if(jTextApellido.getText().trim().equalsIgnoreCase(""))
                JOptionPane.showMessageDialog(null, "Este Campo Es Requerido");
            else {
                jTextDireccion.setEnabled(true);
                jTextDireccion.requestFocus();
            }
        }
    }//GEN-LAST:event_jTextApellidoKeyPressed

    private void jTextDireccionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextDireccionKeyPressed
        if(evt.getKeyCode()==10)
          if(jTextDireccion.getText().trim().equalsIgnoreCase("")){
               JOptionPane.showMessageDialog(null, "Este Campo Es Requerido");
          }else{
               jTextTelefono.setEnabled(true);
               jTextTelefono.requestFocus();
          }
    }//GEN-LAST:event_jTextDireccionKeyPressed

    private void jTextTelefonoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextTelefonoKeyPressed
          if(evt.getKeyCode()==10)
              jTextCodigoSucursal.requestFocus();
    }//GEN-LAST:event_jTextTelefonoKeyPressed

    private void jTextCodigoSucursalKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextCodigoSucursalKeyPressed
        if(evt.getKeyCode()==10){
            try {
                clickCodigoSucursal();
            } catch (SQLException ex) {
                Logger.getLogger(VendedorRegistrar.class.getName()).log(Level.SEVERE, null, ex);
            }                
        }
    }//GEN-LAST:event_jTextCodigoSucursalKeyPressed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        clean();
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jButtonCancelarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButtonCancelarKeyPressed
        if(evt.getKeyCode()==10)
            clean();
    }//GEN-LAST:event_jButtonCancelarKeyPressed

    private void jTextCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextCodigoKeyPressed
        if(evt.getKeyCode()==10)
          if(jTextCodigo.getText().trim().equalsIgnoreCase("")){
               JOptionPane.showMessageDialog(null, "Este Campo Es Requerido");
          }else{
               jTextCedula.setEnabled(true);
               jTextCedula.requestFocus();
          }
    }//GEN-LAST:event_jTextCodigoKeyPressed

    private void jButtonRegistrarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButtonRegistrarKeyPressed
        if(evt.getKeyCode()==10){
            try {
                if(!vendedor.isExist(jTextCodigo.getText()))
                    registrar();
                else{
                    JOptionPane.showMessageDialog(null,"Vendedor Se Encuentra Registrado En El Sistema...!");
                }
            } catch (SQLException ex) {
                Logger.getLogger(VendedorConsultar.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButtonRegistrarKeyPressed

   


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonRegistrar;
    private javax.swing.JButton jButtonSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelSucursal;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JTextField jTextApellido;
    private javax.swing.JTextField jTextCedula;
    private javax.swing.JTextField jTextCodigo;
    private javax.swing.JTextField jTextCodigoSucursal;
    private javax.swing.JTextField jTextDireccion;
    private javax.swing.JTextField jTextNombre;
    private javax.swing.JTextField jTextTelefono;
    // End of variables declaration//GEN-END:variables

    /**
     * Metodo en sobreescritura, el cual recibe informacion de un formulario de consulta.
     * @param texto
     */
    public void setName(String texto){
        jTextCodigoSucursal.setText(texto);
        jTextCodigoSucursal.requestFocus();
        try {
            clickCodigoSucursal();
        } catch (SQLException ex) {
            Logger.getLogger(VendedorRegistrar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Metodo para limpiar todos los campos de texto del formulario.
     */
    private void clean(){//limpio todos los campos de la vista
        jTextCodigo.setEnabled(true);
        jTextCodigo.setText("");
        jTextCedula.setEnabled(true);
        jTextCedula.setText("");
        jTextApellido.setEnabled(false);
        jTextApellido.setText("");
        jTextDireccion.setEnabled(false);
        jTextDireccion.setText("");
        
        jTextCodigoSucursal.setEnabled(false);
        jTextCodigoSucursal.setText("");
        jTextNombre.setEnabled(false);
        jTextNombre.setText("");
        jTextTelefono.setEnabled(false);
        jTextTelefono.setText("");
        jButtonRegistrar.setEnabled(false);
        jTextCedula.requestFocus();
    }

    /**
     * Metodo para consultar y verificar sucursales existentes
     */
    private void clickCodigoSucursal() throws SQLException{  
        if(!sucursal.isExist(jTextCodigoSucursal.getText())){
              if(jTextCodigoSucursal.getText().trim().equalsIgnoreCase("")){
                if(JOptionPane.showConfirmDialog(null,"Desea Consultar Oficinas Sucursales")==0){// RESPONDE SI
                     SucursalConsultar menuConsulta = new SucursalConsultar(rootPaneCheckingEnabled,this);
                     menuConsulta.setVisible(true);
                }
              }else
                  JOptionPane.showMessageDialog(null,"Sucursal No Se Encuentra Registrada En El Sistema...!");                  
        }else{
            Sucursal sucu=new Sucursal();
            sucu=sucursal.getSucursalCodigo(jTextCodigoSucursal.getText());
            jLabelSucursal.setText(sucu.getCiudad());
            jButtonRegistrar.setEnabled(true);
            jButtonRegistrar.requestFocus();
        }
      }
            
     

     private void registrar(){//utilisado para verificar campos vacios y el registro del vendedor
           if(jTextCodigo.getText().trim().equalsIgnoreCase("") ||
                  jTextCedula.getText().trim().equalsIgnoreCase("")||
                  jTextNombre.getText().trim().equalsIgnoreCase("")||
                  jTextApellido.getText().trim().equalsIgnoreCase("")||
                  jTextDireccion.getText().trim().equalsIgnoreCase("")||
                  jTextCodigoSucursal.getText().trim().equalsIgnoreCase("")){
               JOptionPane.showMessageDialog(null, "Uno De Los Campos Es Requerido");
            }else{

                  Vendedor vend=new Vendedor();
                  vend.setCodigo(jTextCodigo.getText());
                  vend.setCedula(jTextCedula.getText());
                  vend.setNombre(jTextNombre.getText());
                  vend.setApellido(jTextApellido.getText());
                  vend.setDireccion(jTextDireccion.getText());
                  vend.setTelefono(jTextTelefono.getText());
                  vend.setCodigoSucursal(jTextCodigoSucursal.getText());
                  vend.setEstado(true);

                  try {
                     vendedor.setNuevoVendedor(vend);
                      JOptionPane.showMessageDialog(null,"Operacion Exitosa...");
                  } catch (SQLException ex) {
                      Logger.getLogger(VendedorRegistrar.class.getName()).log(Level.SEVERE, null, ex);
                  }
                  clean();
            }
    }

}
